package com.qwc.blog.controller.back;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qwc.blog.entity.Tag;
import com.qwc.blog.entity.vo.tag.AddTagDto;
import com.qwc.blog.entity.vo.tag.TagListReqDto;
import com.qwc.blog.entity.vo.tag.TagVo;
import com.qwc.blog.security.service.LoginService;
import com.qwc.blog.service.ITagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.qwc.blog.common.constant.ResponseConstant.*;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author qwc
 * @since 2021-10-30
 */
@RestController
@RequestMapping("/blog/tag")
public class TagController {

    @Autowired
    private ITagService tagService;

    @Autowired
    private LoginService loginService;

    @GetMapping("/list")
    public ResponseEntity<Object> list(TagListReqDto dto) {
        Map<String, Object> map = new HashMap<>(4);
        Page<Tag> page = tagService.getTagList(dto);
        map.put(ROWS, page.getRecords());
        map.put(TOTAL, page.getTotal());
        return ResponseEntity.ok(map);
    }

    @PostMapping
    public ResponseEntity<Object> addTag(@RequestBody AddTagDto dto) {
        tagService.addTag(dto.createTag(), loginService.getCurrentUsername());
        return ResponseEntity.ok().build();
    }

    @DeleteMapping("/{ids}")
    public ResponseEntity<Object> deleteTag(@PathVariable Integer[] ids) {
        tagService.removeByIds(Arrays.asList(ids));
        return ResponseEntity.ok().build();
    }

    @GetMapping("/all")
    public ResponseEntity<Object> allTags() {
        Map<String, Object> map = new HashMap<>(4);
        List<TagVo> collect = tagService.list().stream().map(TagVo::createTagVo).collect(Collectors.toList());
        map.put(DATA, collect);
        return ResponseEntity.ok(map);
    }

}
